/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年4月13日 下午4:14:46
 */
package com.absir.appserv.slave.api;

import java.io.IOException;

import com.absir.appserv.crud.ICrudSupply;
import com.absir.appserv.slave.bean.JServer;
import com.absir.appserv.slave.service.SlaveVersionService;
import com.absir.appserv.system.bean.value.JaLang;
import com.absir.appserv.system.helper.HelperDatabind;
import com.absir.appserv.system.server.value.Bodys;
import com.absir.appserv.system.service.BeanService;
import com.absir.appserv.system.service.CrudService;
import com.absir.bean.basis.Base;
import com.absir.server.value.Server;

/**
 * @author absir
 *
 */
@Base
@Server
public class api_slave extends ApiSlave {

	@JaLang("时间")
	public long time() {
		return System.currentTimeMillis();
	}

	@JaLang("同步服务")
	public void sync(@Bodys JServer server) {
		BeanService.ME.merge(server);
	}

	@JaLang("同步服务实体")
	public void merge(String entityName, @Bodys byte[] postData) throws IOException {
		option(entityName, 0, postData);
	}

	@JaLang("同步服务实体")
	public void option(String entityName, int option, @Bodys byte[] postData) throws IOException {
		ICrudSupply crudSupply = CrudService.ME.getCrudSupply(entityName);
		Class<?> entityClass = crudSupply.getEntityClass(entityName);
		Object entity = HelperDatabind.read(postData, 0, postData.length, entityClass);
		if (option == 0) {
			crudSupply.mergeEntity(entityName, entity, false);

		} else if (option == 1) {
			crudSupply.mergeEntity(entityName, entity, true);

		} else {
			crudSupply.deleteEntity(entityName, entity);
		}
	}

	@JaLang("版本升级")
	public void upgrade(String upgradeFile) throws Exception {
		SlaveVersionService.ME.upgrade(upgradeFile);
	}

	@JaLang("资源升级")
	public void resource(String resourceFile) throws Exception {
		SlaveVersionService.ME.resource(resourceFile);
	}
}
